package com.xiaogao.serviceEdu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaogao.serviceBase.utils.exceptionHandler.XiaoGaoException;
import com.xiaogao.serviceEdu.entity.EduChapter;
import com.xiaogao.serviceEdu.entity.EduVideo;
import com.xiaogao.serviceEdu.entity.vo.ChapterVo;
import com.xiaogao.serviceEdu.entity.vo.VideoVo;
import com.xiaogao.serviceEdu.mapper.EduChapterMapper;
import com.xiaogao.serviceEdu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaogao.serviceEdu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程章节 服务实现类
 * </p>
 *
 * @author 小高
 * @since 2021-06-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;

    @Override
    public List<ChapterVo>  getChapterVideoByCourseId(String courseId) {
        // 最终要得到的数据
        List<ChapterVo> chapterVoList = new ArrayList<>();
        // 获取章节信息
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper();
        wrapperChapter.eq("course_id", courseId);
        wrapperChapter.orderByAsc("sort", "id");
        List<EduChapter> chapterList = baseMapper.selectList(wrapperChapter);
        // 获取课时信息
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id", courseId);
        wrapperVideo.orderByAsc("sort", "id");
        List<EduVideo> videoList = videoService.list(wrapperVideo);

        // 封装章节vo数据
        for (int i = 0; i < chapterList.size(); i++) {
            EduChapter eduChapter = chapterList.get(i);
            // 创建章节vo对象
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            chapterVoList.add(chapterVo);

            // 封装课时vo数据
            ArrayList<VideoVo> videoVoArrayList = new ArrayList<>();
            for (int j = 0; j < videoList.size(); j++) {
                EduVideo eduVideo = videoList.get(j);
                if (eduChapter.getId().equals(eduVideo.getChapterId())) {
                    // 创建课时vo对象
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoArrayList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoArrayList);
        }
        return chapterVoList;
    }

    // 根据id删除章节
    @Override
    public boolean removeChapterById(String id) {

        // 根据id查询该章节是否存在视频，如果存在则提示用户尚有子节点
        if (videoService.getChapterMenuById(id)){
            throw new XiaoGaoException(20001,"该章节下存在视频课程，请先删除视频课程！");
        }

        return this.removeById(id);
    }

    @Override
    public boolean removeCourseById(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        Integer count = baseMapper.delete(wrapper);
        return null != count && count > 0;
    }
}
